<template>
  <div>
    <el-form ref="form"
             :model="form"
             label-width="80px"
             class="frame">
      <el-form-item label="用户头像"
                    prop="userHeadImage">

        <el-upload class="avatar-uploader"
                   action="http://localhost:8888/uploadss"
                   name="file"
                   :headers="upload.headerObj"
                   :show-file-list="false"
                   :on-success="handleSuccess"
                   :before-upload="beforeUpload">
          <el-avatar :src="this.form.userHeadImage"></el-avatar>
          <!-- <img v-if="imageUrl"
               :src=""
               class="avatar"
               size="20px" />
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i> -->
        </el-upload>
      </el-form-item>
      <!--头像上传-->
      <el-form-item label="昵称">
        <el-input placeholder="请输入内容"
                  v-model="form.userName"
                  clearable></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input placeholder="请输入年龄"
                  v-model="form.userAge"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input placeholder="我的邮箱"
                  v-model="form.userEmail"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input placeholder="真实姓名"
                  v-model="form.userRealName"></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input placeholder="身份证号"
                  v-model="form.userIdCard"></el-input>
      </el-form-item>
      <el-form-item label="出生日期">
        <div class="block">
          <span class="demonstration"></span>
          <el-date-picker v-model="form.userBirthday"
                          type="date"
                          placeholder="选择日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="submitForm">提交</el-button>

        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
  
  <script>
export default {
  data () {
    return {
      // userHeadImage: "",
      // imageUrl: "",
      form: {

      },
      upload: {
        headerObj: {
          //Spring security时需要传递token，否则不需要该配置
          // Authorization: sessionStorage.getItem("authorization"),
        },
      },

    };
  },
  methods: {
    //    handleEdit() {
    //   this.$axios.get("users/"+this.userId).then((res) => {
    //     this.form = res.data.data.user;

    //   });
    // },
    message () {
      console.log(this.userId);
      this.$axios.get("users/" + this.userId).then((resp) => {
        this.form = resp.data.data.user;
      });

    },
    beforeUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    handleSuccess (res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.form.userHeadImage = res.message;
    },
    //修改个人信息--wu
    submitForm () {
      this.$axios.put("users", this.form).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.messages);
          this.message();

        } else {
          this.$message.error(res.data.messages);
        }
      });
    },
  },
  created () {
    this.userId = JSON.parse(sessionStorage.getItem("userId"));
    console.log(this.userId);
    this.message();

  }

};

</script>
<style scoped>
.el-select {
  width: 70px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.avatar {
  width: 50px;
  height: 50px;
  display: block;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.frame {
  width: 370px;
}
</style>